package com.bibrApp.difu.utils;

import android.text.TextUtils;

import java.math.BigDecimal;

public class BigdecimalUtils {
    public static String format(float value) {
        if (value > 100) {
            return String.format("%.2f", value);
        } else if (value > 10 && value < 100) {
            return String.format("%.3f", value);
        } else if (value > 1 && value < 10) {
            return String.format("%.4f", value);
        } else if (value > 0.0001 && value < 1) {
            return String.format("%.6f", value);
        } else if (value > 0.00001 && value < 0.0001) {
            return String.format("%.7f", value);
        } else if (value > 0.000001 && value < 0.00001) {
            return String.format("%.8f", value);
        } else if (value > 0.0000001 && value < 0.000001) {
            return String.format("%.9f", value);
        } else if (value > 0.00000001 && value < 0.0000001) {
            return String.format("%.10f", value);
        }
        return String.format("%.2f", value);
    }

    /**
     * 根据指定小数位数返回对应的字符串
     * @return
     */
    public static String format2Str(double doubleNum ,int num){
        return String.format("%."+num+"f", doubleNum);
    }

    /**
     * 获取小数点后位数
     *
     * @param str
     * @return
     */
    public static int getPointNum(String str) {
        if (!TextUtils.isEmpty(str)) {
            if (str.contains(".")) {
                return (str).length() - (str).indexOf(".") - 1;
            } else {
                return 0;
            }
        }
        return 0;
    }

    /**
     * 将字符串转换成相应位数的double类型
     */
    public static double getPrice(String str, int num) {
        if (TextUtils.isEmpty(str)) {
            return 0;
        }

        Double a = Double.parseDouble(str);
        BigDecimal bd = new BigDecimal(a);
        Double d = bd.setScale(num, BigDecimal.ROUND_DOWN).doubleValue();

        return d;
    }

    /**
     * 将字符串转换成相应位数的BigDecimal类型
     */
    public static BigDecimal getBigDecimalPrice(String str, int num) {
        if (TextUtils.isEmpty(str) || str == null) {
            throw new NumberFormatException("str is null!");
        }
        try{
            int pointNum = getPointNum(str);
            if (pointNum < num) {
                num = pointNum;
            }
            //由于double类型不精准，则直接用str转成BigDecimal
            Double a = Double.parseDouble(str);
            BigDecimal bd = new BigDecimal(a);
 
            return bd.setScale(num, BigDecimal.ROUND_DOWN);
        }catch (NumberFormatException e){
            throw new NumberFormatException("str is wrong!");
        }
    }

    public static BigDecimal getBigDecimalPointNum(BigDecimal bigDecimal,int num){
        if(bigDecimal == null){
            throw new NumberFormatException("bigDecimal is null!");
        }
        return bigDecimal.setScale(num, BigDecimal.ROUND_DOWN);
    }

    /**
     * 将字符串转换成相应位数的BigDecimal类型 高精度
     */
    public static BigDecimal getHighBigDecimalPrice(String str, int num) {
        if (TextUtils.isEmpty(str) || str == null) {
            throw new NumberFormatException("str is null!");
        }
        try{
            int pointNum = getPointNum(str);
            if (pointNum < num) {
                num = pointNum;
            }
            //由于double类型不精准，则直接用str转成BigDecimal
            BigDecimal bd = new BigDecimal(str);

            return bd.setScale(num, BigDecimal.ROUND_DOWN);
        }catch (NumberFormatException e){
            throw new NumberFormatException("str is wrong!");
        }
    }
}
